package com.xywy.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.os.Environment;

import com.xywy.common.bean.ConnectedDevice;
import com.xywy.me.bean.Jtcy;

public class FileUtil {
	public static final String split= "/xywy/";
	public static final String CACHEUSER = "usercache";
	
	public static final String FWC_MESSAGES = "fwcmessagelist"; //服务窗外部缓存
	public static final String FWC_BOTTOM_MENU = "fwcbottommenu";//服务窗底部缓存
	public static final String FWC_INNER_LIST = "fwcinnerlist"; //服务窗内部缓存
	public static final String FWC_USER = "fwcuser"; //服务窗内部缓存
	public static final String JKZH = "jkzh"; //服务窗内部缓存
	
	public static final String DEVICE_WEIGHT = "DEVICE_WEIGHT"; //连接的体重
	public static final String DEVICE_BLOOD = "DEVICE_BLOOD"; //连接的血压
	public static final String DEVICE_TEMPUTER = "DEVICE_TEMPUTER"; //连接的体温计
	
	public static void saveObjectList(String fileName,List list) {
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(new File(fileStr));
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(list);
			os.flush();
			os.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	public static void clearFile(String fileName){
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			file.delete();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取数据
	 * @param fileName
	 */
	public static List getList(String fileName){
		List<Object> list = new ArrayList<Object>();
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileInputStream fins = new FileInputStream(new File(fileStr));
			ObjectInputStream oins = new ObjectInputStream(fins);
			list = (List<Object>) oins.readObject();
			oins.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return list;
	}
	
    public static void saveObject(String fileName, Object object){
    	try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(new File(fileStr));
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(object);
			os.flush();
			os.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
    }
    
    public static Object getObject(String fileName){
		Object object = null;
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileInputStream fins = new FileInputStream(new File(fileStr));
			ObjectInputStream oins = new ObjectInputStream(fins);
			object =  oins.readObject();
			oins.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return object;
	}
	
	
	/**
	 * 
	 * @param fileName
	 * @param user
	 */
	
	public static void saveCurrentUser(String fileName,Jtcy user){
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(new File(fileStr));
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(user);
			os.flush();
			os.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 缓存数据
	 * @param fileName
	 */
	public static Jtcy getCurrentUser(String fileName){
		Jtcy jc = new Jtcy();
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileInputStream fins = new FileInputStream(new File(fileStr));
			ObjectInputStream oins = new ObjectInputStream(fins);
			jc = (Jtcy) oins.readObject();
			oins.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
		return jc;
	}
	
	public static long getFileTime(String fileName){
		String fileStr = Environment.getExternalStorageDirectory()+split+fileName;
		File file = new File(fileStr);
		if(file.exists() ){
			return file.lastModified();
		}else {
			return -1;
		}
	}
	
	public static void clear(File file){
		if (file.isFile()) {  
			           file.delete();  
			            return;  
			         }  
			  
			        if(file.isDirectory()){  
			            File[] childFiles = file.listFiles();  
			            if (childFiles == null || childFiles.length == 0) {  
			                file.delete();  
			                return;  
			            }  
			      
			            for (int i = 0; i < childFiles.length; i++) {  
			                clear(childFiles[i]);  
			            }  
			           file.delete();  
			        }  
	}
	
	/**
	 * 保存连接体重的信息
	 * 存储体重设备
	 * @param connected 是否连接
	 */
	public static void saveWeightDevice(ConnectedDevice connect_device){
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_WEIGHT;
			File file = new File(fileStr);
			if(file.exists()){
				file.delete();
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(new File(fileStr));
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(connect_device);
			os.flush();
			os.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	
	/**
	 * 存储体温设备信息
	 * @return
	 */
	
	public static void saveTemputerDevice(ConnectedDevice connect_device){
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_TEMPUTER;
			File file = new File(fileStr);
			if(file.exists()){
				file.delete();
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(new File(fileStr));
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(connect_device);
			os.flush();
			os.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 存储血压设备信息
	 * @return
	 */
	
	public static void saveBloodDevice(ConnectedDevice connect_device){
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_BLOOD;
			File file = new File(fileStr);
			if(file.exists()){
				file.delete();
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(new File(fileStr));
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(connect_device);
			os.flush();
			os.close();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	
	/**
	 * 获得体重的连接对象
	 * @return
	 */
	public static ConnectedDevice getWeightDevice(){
		ConnectedDevice connect_device = new ConnectedDevice();
		connect_device.setConnect_time(0);
		connect_device.setConnected(false);
		connect_device.setDevice_name(ConnectedDevice.WEIGHT_NAME);
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_WEIGHT;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileInputStream fins = new FileInputStream(new File(fileStr));
			ObjectInputStream oins = new ObjectInputStream(fins);
			connect_device =  (ConnectedDevice) oins.readObject();
			oins.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return connect_device;
		}
		return connect_device;
	}
	
	/**
	 * 获得血压设备
	 * @return
	 */
	public static ConnectedDevice getBloodDevice(){
		ConnectedDevice connect_device = new ConnectedDevice();
		connect_device.setConnect_time(0);
		connect_device.setConnected(false);
		connect_device.setDevice_name(ConnectedDevice.BLOOD_NAME);
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_BLOOD;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileInputStream fins = new FileInputStream(new File(fileStr));
			ObjectInputStream oins = new ObjectInputStream(fins);
			connect_device =  (ConnectedDevice) oins.readObject();
			oins.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return connect_device;
		}
		return connect_device;
	}
	
	/**
	 * 获得血压设备
	 * @return
	 */
	public static ConnectedDevice getTemputerDevice(){
		ConnectedDevice connect_device = new ConnectedDevice();
		connect_device.setConnect_time(0);
		connect_device.setConnected(false);
		connect_device.setDevice_name(ConnectedDevice.TEMPUTER_NAME);
		try {
			String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_TEMPUTER;
			File file = new File(fileStr);
			if(file.exists()){
				
			}else {
				File parentF = file.getParentFile();
				parentF.mkdir();
				file.createNewFile();
			}
			FileInputStream fins = new FileInputStream(new File(fileStr));
			ObjectInputStream oins = new ObjectInputStream(fins);
			connect_device =  (ConnectedDevice) oins.readObject();
			oins.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return connect_device;
		}
		return connect_device;
	}
	
	/**
	 * 获得连接的设备
	 * @return
	 */
	public static ConnectedDevice getConnectedDeivce(){
		ConnectedDevice weight_device = getWeightDevice();
		ConnectedDevice blood_device = getBloodDevice();
		ConnectedDevice temp_device = getTemputerDevice();
	    if(weight_device.isConnected()){
	    	return weight_device;
	    }else if (blood_device.isConnected()) {
			return blood_device;
		}else if(temp_device.isConnected()){
			return temp_device;
		}else {
			return null;
		}
	}
	
	/**
	 * 获得当前设备
	 * @return
	 */
	public static ConnectedDevice getCurrentDevice(){
		ConnectedDevice weight_device = getWeightDevice();
		ConnectedDevice blood_device = getBloodDevice();
		ConnectedDevice temp_device = getTemputerDevice();
		System.out.println("weight---shijian---"+weight_device.getConnect_time());
		System.out.println("blood---shijian---"+blood_device.getConnect_time());
		System.out.println("temp---shijian---"+temp_device.getConnect_time());
		List<ConnectedDevice> devices = new ArrayList<ConnectedDevice>();
		devices.add(weight_device);
		devices.add(blood_device);
		devices.add(temp_device);
		Collections.sort(devices, new DeviceCompator());
		return devices.get(0);
	}
	
	/**
	 * 存储设备信息
	 * @param fileName
	 * @param object
	 */
//	   public static void saveDevices(List<ConnectedDevice> object){
//	    	try {
//				String fileStr = Environment.getExternalStorageDirectory()+split+DEVICE_LIST;
//				File file = new File(fileStr);
//				if(file.exists()){
//					
//				}else {
//					File parentF = file.getParentFile();
//					parentF.mkdir();
//					file.createNewFile();
//				}
//				FileOutputStream fos = new FileOutputStream(new File(fileStr));
//				ObjectOutputStream os = new ObjectOutputStream(fos);
//				os.writeObject(object);
//				os.flush();
//				os.close();
//				
//			} catch (Exception e) {
//				// TODO: handle exception
//				e.printStackTrace();
//			}
//	    }
	
	static class DeviceCompator implements Comparator<ConnectedDevice>{

		@Override
		public int compare(ConnectedDevice device1, ConnectedDevice device2) {
			if(device1.getConnect_time() >device2.getConnect_time()){
				return -1;
			}else if (device1.getConnect_time() <device2.getConnect_time()) {
				return 1;
			}else {
				return 0;
			}
		}
		
	}
}
